import Toast from "@vant/weapp/toast/toast";
Page({
  data: {
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      }
      if (type === 'month') {
        return `${value}`;
      }
      return value;
    },
    show: false,
    showc: 0,
    house: '请选择房屋信息',
    repairItem: '请选择维修项目',
    now: '请选择上门维修日期',
    form: {
      mobile: '',
      attachment: [],
    },
  },
  async sub() {
    if (!(/^1[3-9]\d{9}$/.test(this.data.form.mobile))) return Toast('手机号格式错误');
    if (!this.data.form.houseId) return Toast('请选择房屋信息');
    let res = await wx.http.post('/repair', this.data.form)
    Toast(res.message)
    if (res.code == 10000) {
      wx.navigateTo({
        url: '/pages/mybaoxiu/mybaoxiu'
      })
    }
  },
  afterRead(event) {
    let that = this
    const { file } = event.detail;
    wx.uploadFile({
      url: 'https://live-api.itheima.net/upload',
      filePath: file.url,
      name: 'file',
      header: {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'Bearer ' + wx.getStorageSync('token'),
      },
      formData: { user: 'test' },
      success(res) {
        const fileList = that.data.form.attachment;
        fileList.push({ url: JSON.parse(res.data).data.url, id: JSON.parse(res.data).data.id, });
        that.setData({
          'form.attachment': fileList
        });
      },
    });
  },
  onInput(event) {
    this.setData({
      now: this.gettime(event.detail, '/'),
      'form.appointment': this.gettime(event.detail, '-'),
      show: false,
    });
  },
  textfn(e) {
    this.setData({
      'form.description': e.detail.value
    })
  },
  mobilefn(e) {
    this.setData({
      'form.mobile': e.detail.value
    })
  },
  viewTap: function (e) {
    this.setData({
      'form.houseId': e.currentTarget.dataset.item.id,
      house: e.currentTarget.dataset.item.point + e.currentTarget.dataset.item.building + e.currentTarget.dataset.item.room,
      show: false
    })
  },
  viewTap2: function (e) {
    this.setData({
      'form.repairItemId': e.currentTarget.dataset.item.id,
      repairItem: e.currentTarget.dataset.item.name,
      show: false
    })
  },
  datafn() {
    this.setData({
      show: true,
      showc: 3
    })
  },
  weixinfn() {
    this.setData({
      show: true,
      showc: 2
    })
  },
  showfn() {
    this.setData({
      show: true,
      showc: 1
    })
  },
  close() {
    this.setData({
      show: false
    })
  },
  async onLoad() {
    let res = await wx.http.get('/room')
    let arr = res.data.filter(item => item.status == 2)
    this.setData({
      list: arr
    })
    let res2 = await wx.http.get('/repairItem')
    this.setData({
      list2: res2.data
    })
  },
  gettime(time, t) {
    const timestamp = time;
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const formattedDate = year + t + month + t + day;
    return formattedDate
  }
})